<?php 
set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

require_once("config.php");
require_once("functions.php");

if (!$allow_url_fopen) {
	echo '<span class="error"><b>Note</b>: Please enable \'<em>allow_url_fopen</em>\' option in PHP configuration file</span><br/>';
}

require_once("Google.php");

$g = new Google();

$temp_pageurls = $_REQUEST['pageurls'];
$temp_keywords = $_REQUEST['keywords'];
$num = intval(@$_REQUEST['num']);

$pageurls = array();
$keywords = array();

if (empty($num)) $num = 1;

$temp = explode('~@~',$temp_pageurls);

foreach($temp as $val) {

	if (!empty($val)) {
		$pageurls[] = $val;
	}
}

$temp = explode('~@~',$temp_keywords);

foreach($temp as $val) {

	if (!empty($val)) {
		$keywords[] = $val;
	}
}

$results = array();
$count = 0;

if ( !empty($pageurls) && !empty($keywords) ) {

	foreach ( $keywords as $keyword ) {

		$pr = $start = ($num-1) * 100;
		$total = $g->get_response(urlencode($keyword)."&start=".$start."&num=100");

		if ($start >= $total) continue;

		$links = $g->get_links(urlencode($keyword)."&start=".$start."&num=100");
//echo "<pre>"; print_r($links);

		$old_pr = $pr;
		
		foreach ( $pageurls as $url ) {

			$pr = $old_pr;

			foreach ($links as $link) {

				$pr++;
				$link_link = $link['link'];
				if (strpos($link['link'], '?') !== false) {
					$link_link = substr($link['link'], 0, strpos($link['link'], '?')+1 );
				}
				if (strpos($link_link, $url) !== false) {

					$results[$count]['link'] = $link['link'];
					$results[$count]['url'] = $url;
					$results[$count]['key'] = $keyword;
					$results[$count++]['pr'] = $pr;
				}

			} //foreach
		}
	}
}


$k = 1;
foreach ($results as $details ) {
?>
  <tr class="tr<?php echo (($k % 2 == 0)?'1':'2');  ?>">
    <td align="left"><?php echo $details['url']; ?></td>
    <td align="left"><?php echo $details['key']; ?></td>
	<td align="center"><?php echo $details['pr']; ?></td>
	<td align="left"><?php echo $details['link']; ?></td>
  </tr>
<?php
}
?>